﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Sql
{
    public partial class Form6 : Form
    {
        string Read_name;
        public Form6(string name)
        {
            InitializeComponent();
            Read_name = name;
        }
        void BindGrid()
        {
            try
            {
                SQL.Open();
                SqlCommand lo_cmd = new SqlCommand();
                lo_cmd.CommandText = "select * from Reader";
                lo_cmd.Connection = SQL.conn;
                lo_cmd.ExecuteNonQuery();
                SqlDataAdapter dbAdapter = new SqlDataAdapter(lo_cmd);
                DataSet ds = new DataSet();
                dbAdapter.Fill(ds);
                SQL.Close();
                dataGridView1.DataSource = ds.Tables[0];
            }
            catch
            {
                MessageBox.Show("异常");
            }

        }

        private void Form6_Load(object sender, EventArgs e)
        {
            label2.Text = Read_name;
            BindGrid();
            timer1.Start();
            timer1_Tick(null, null);
        }
        void getselectdata()
        {
            int a = dataGridView1.CurrentRow.Index;
            textBox1.Text = dataGridView1.Rows[a].Cells["R_no"].Value.ToString();
            

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            getselectdata();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string sqlStr = "update Reader set R_name='" + textBox2.Text
                + "',R_sex='" + textBox3.Text + "',R_age=" + textBox4.Text + ",R_unit='" + textBox5.Text + "',R_tel='" + textBox6.Text
                + "'where R_no='" + textBox1.Text + "'";
            if (SQL.SqlUpdate(sqlStr))
                MessageBox.Show("更新成功");
            Form6_Load(this, null);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string sqlInsert = "insert into Reader values('" + textBox7.Text + "','" + textBox8.Text + "','" + textBox9.Text + "'," + textBox10.Text + " ,'"+ textBox11.Text + "','"+textBox12.Text + "')";
            if(SQL.SqlInsert(sqlInsert))
                MessageBox.Show("添加成功");
            Form6_Load(this, null);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string sqlStr = "delete from Reader where R_no='" + textBox1.Text + "'";
            if(SQL.SqlDelete(sqlStr))
                MessageBox.Show("删除成功");
            Form6_Load(this, null);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Form9 form = new Form9(Read_name);
            form.Show();
            this.Hide();
        }

        private void Form6_FormClosing(object sender, FormClosingEventArgs e)
        {
            Application.Exit();
        }
    }
}
